package com.sz.app.oss.api.logistics.price;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.ModuleNames;
import com.sz.app.oss.constants.Permissions;
import com.sz.app.oss.exception.ErrorCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.core.dto.QuotationDetailCopyDto;
import com.sz.biz.logistics.core.entity.QuotationSet;
import com.sz.biz.logistics.core.service.QuotationService;
import com.sz.biz.logistics.core.dto.QuotationDto;
import com.sz.biz.logistics.core.dto.QuotationSetDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.core.service.PrincipalLogUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * Function: 报价方案. <br>
 * Date: 2017年03月20日 14:35 <br>
 * Author: wentao.chang
 */
@RestController
@RequestMapping("/api/v1/biz/quotations/sale")
@Api(description = " ", tags = "035-003、价格-报价方案管理-销售报价方案管理")
public class SaleQuotationController extends BaseController {

    @Autowired
    private QuotationService quotationService;

    @ApiOperation(value = "保存销售报价方案的信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quotationDto", value = "" +
                    "type(2:销售价),feeTypeId,name,bizuid,description,\r\n" +
                    "startEffectTime,endEffectTime,\r\n" +
                    "regionSchemaUid,weightSchemaUid,\r\n" +
                    "grade,settlementTypeId,cargoTypeCode,\r\n" +
                    "currencyId,\r\n" +
                    "areas:\r\n" +
                    "[{figureCode}]\r\n" +
                    "}", required = true, dataType = "ServiceDto", paramType = "body")
    })
    @RequestMapping(value = "/main", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto saveMain(@RequestBody QuotationDto quotationDto) throws ParseException {
        QuotationDto qu = quotationService.saveMain(quotationDto);
        ResultDto dto = saveDraftSuccessDto();
        dto.setData(qu);
        return dto;
    }


    @ApiOperation(value = "更新销售报价方案的信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quotationDto", value = "" +
                    "type(2:销售价),feeTypeId,name,bizuid,description,\r\n" +
                    "startEffectTime,endEffectTime,\r\n" +
                    "regionSchemaUid,weightSchemaUid,\r\n" +
                    "grade,settlementTypeId,cargoTypeCode,\r\n" +
                    "currencyId,\r\n" +
                    "areas:\r\n" +
                    "[{figureCode}]\r\n" +
                    "}", required = true, dataType = "ServiceDto", paramType = "body")
    })
    @RequestMapping(value = "{uid}/main", method = {RequestMethod.PUT})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto updateMain(@PathVariable String uid, @RequestBody QuotationDto quotationDto) throws ParseException {
        QuotationDto qu = quotationService.updateMain(uid, quotationDto, false);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_PRICE);
        dto.setData(qu);
        return dto;
    }

    @ApiOperation(value = "复制销售报价方案的明细信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dto", value = "" +
                    "{fromUid , toUids[]}", required = true, dataType = "QuotationDetailCopyDto", paramType = "body")
    })
    @RequestMapping(value = "{uid}/copy", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto updateMain(@PathVariable String uid, @RequestBody QuotationDetailCopyDto dto) throws ParseException {
        QuotationDto q = quotationService.copyDetail(uid, dto);
        ResultDto res =  getUpdateSuccessDto(MessagesCodes.MODULE_PRICE);
        res.setData(q);
        return res;
    }

    @ApiOperation(value = "获取单个报价方案主要信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "string",
                    paramType = "path", required = true)
    })
    @RequestMapping(value = "/{uid}", method = {RequestMethod.GET})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto online(@PathVariable String uid) {
        QuotationDto dto = quotationService.findActivedByUid(uid);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "获取报价方案某个明细信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_COST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "string",
                    paramType = "path", required = true),
            @ApiImplicitParam(name = "id", value = "明细id", dataType = "int",
                    paramType = "path", required = true)
    })
    @RequestMapping(value = "/{uid}/detail/{id}", method = {RequestMethod.GET})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto online(@PathVariable String uid, @PathVariable int id) {
        QuotationSetDto dto = quotationService.findQuotationSetBySetId(uid, id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "保存报价方案的明细信息", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "方案uid", dataType = "string", paramType = "path", required = true),
            @ApiImplicitParam(name = "quotationSetDto", value = "" +
                    "weightSchemaUid,feeTypeId,supplierId,serviceUid,id,quotationId," +
                    "List<FreightItem> freightItems-明细:\r\n" +
                    "FreightItem{\r\n" +
                    "toRegionUid:目的分区\r\n" +
                    "price:价格\r\n" +
                    "calcType:计价方式code\r\n" +
                    "baseValue:价格单位\r\n" +
                    "baseUnitCode：价格计量单位}\r\n" +
                    "List<FreightWeightSetting> freightWeightSettings-重量段设置:\r\n" +
                    "FreightWeightSetting{" +
                    "calcType:计价方式code\r\n" +
                    "baseValue:价格单位\r\n" +
                    "baseUnitCode：价格计量单位code}\r\n" +
                    "}", required = true, dataType = "ServiceDto", paramType = "body")
    })
    @RequestMapping(value = "/{uid}/detail", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto saveDetail(@PathVariable String uid, @RequestBody QuotationSetDto quotationSetDto) throws ParseException {
        QuotationSet set = quotationService.saveDetail(uid, quotationSetDto, false);
        ResultDto dto = getUpdateSuccessDto(MessagesCodes.MODULE_PRICE);
        dto.setData(set);
        return dto;
    }


    @ApiOperation(value = "提交审核报价方案", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "方案uid", dataType = "String[]",
                    paramType = "body", required = true)
    })
    @RequestMapping(value = "/submit", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto submit(@RequestBody List<String> uids) throws ParseException {
        quotationService.submit(uids);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.UPDATE,
                "提交审核报价方案：(uids)" + uids);
        return getSubmitSuccessDto(MessagesCodes.MODULE_PRICE);
    }

    @ApiOperation(value = "删除报价方案", notes = "-<br>" + Permissions.BIZ_MGT_BASE_PRICE_SALES)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "方案uid", dataType = "String[]",
                    paramType = "body", required = true)
    })
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_PRICE_SALES, Permissions.BIZ_MGT_BASE_PRICE_SALES_APPROVAL}, logical = Logical.OR)
    public ResultDto delete(@RequestBody String[] uids) {
        if (uids == null || uids.length == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "uids");
        }
        quotationService.delete(Arrays.asList(uids));
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_QUOTATION, UserActions.DELETE,
                "删除报价方案：(uids)" + Arrays.toString(uids));
        return getDeleteSuccessDto(MessagesCodes.MODULE_PRICE);
    }

}
